<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
class Slide extends Model{
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
	// 自动完成
	protected $auto = [];
	protected $insert = [];
	protected $update = [];			
	//图片处理
	protected function setSlidePicAttr($value){
		$img = model('Img');
		$file=$img->down_load(trim($value),'slide');
		return $file['file'];
	}
	//图片处理
	protected function setSlideBigpicAttr($value){
		$img = model('Img');
		$file=$img->down_load(trim($value),'slide');
		return $file['file'];
	}		
	protected function setSlideContentAttr($value){
	    if(!empty($value)){
	       return zanpian_content_down_images(trim($value),'slide');
	    }else{
	       return ""; 	 
	    } 
	}
	//事件注册
    protected static function init(){
		//删除后置操作
        Slide::afterDelete(function ($slide) {
			//删除图片
            if(!empty($slide->slide_pic) && file_exists(ROOT_PATH.$slide->slide_pic)){
                @unlink(ROOT_PATH.$slide->slide_pic);			
            }
			//删除缩略图
            if(!empty($slide->slide_bigpic) && file_exists(ROOT_PATH.$slide->slide_bigpic)){
                @unlink(ROOT_PATH.$slide->slide_bigpic);			
            }			
       });	
    }
    public function listData($tag){
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : 'slide_id,slide_oid,slide_cid,slide_name,slide_pic,slide_bigpic,slide_url,slide_content,slide_vid,vod_id,vod_cid,vod_letters,vod_name,vod_title,vod_pic,vod_aliases,vod_mcid,vod_isend,vod_continu,vod_total,vod_content,vod_year,vod_area,vod_isfilm,vod_filmtime,vod_diantai,vod_tvcont,vod_actor,vod_director';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
        if(!in_array($tag['order'],['slide_id asc','slide_id desc','slide_oid asc','slide_oid desc'])){
            $tag['order'] = 'slide_oid asc';
        }	
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		$where['slide_status']=['eq',1];
		if(!empty($tag['cid'])){
		   $where['slide_cid'] = ['in',$tag['cid']];
		}
		$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
		$data = db('slide')->join('vod vod','vod.vod_id = slide_vid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_link')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }
		return $data;
    } 	
}